﻿@using System.Web.Script.Serialization
@using eCommerce.Core.Domain.Messges
@using eCommerce.Core.Domain.Products

@{
    ViewBag.Title = "title";
}
@Styles.Render("~/content/fineuploader/fineuploader-3.5.0.css")
@Scripts.Render("~/content/fineuploader/fineuploader-3.5.0.js")
@Scripts.Render("~/content/fineuploader/iframe.xss.response-3.5.0.js")

<script src="~/Scripts/json2.js"></script>
<script src="~/Scripts/form2js.js"></script>
@model eCommerce.Models.Product.ItemCatalogsModel
           
<form id="submitform">
    <input type="hidden" name="ProductId" value="@Model.ProductId"/>
    <div>

        <table border="1">
            <tr>

                @if (Model.UseProductAttribute)
                {
                    <th>
                        @Model.ProductAttributeKey.Name
                    </th>
                }
                @if (Model.UseItemAttribute && Model.UseProductAttribute)
                {
                    <th>@Model.ItemAttributeKey.Name</th>
                }
                <th>stock</th>
                <th>price</th>
                <th>icon picture</th>
                <th>product picture</th>
            </tr>

            @{
                var itemCatalogGroupIndex = 0;
            }

            @foreach (var itemCatalogGroup in Model.ItemCatalogGroups)
            {
                <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].ProductAttributeValueId" value="@itemCatalogGroup.ProductAttributeValueId" />
                <tr>
                    @{
                        var firstItemCata = itemCatalogGroup.ItemCatalogs.FirstOrDefault();
                    }
                    <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[0].ProductAttributeValueId" value="@firstItemCata.ProductAttributeValueId" />
                    <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[0].ItemAttributeValueId" value="@firstItemCata.ItemAttributeValueId" />
                    
                    @if (Model.UseProductAttribute)
                    {  
                        <td rowspan="@itemCatalogGroup.ItemCatalogs.Count">
                                            @itemCatalogGroup.ProductAttributeValue.Value
                         </td>
                    }
                  
                  @if (Model.UseItemAttribute)
                  {

                      <td>@firstItemCata.ItemAttributeValue.Value</td>
                  }
                    <td>
                        <input type="text" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[0].Stock" />
                    </td>
                    <td>
                        <input type="text" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[0].Price" />
                    </td>
                    <td rowspan="@itemCatalogGroup.ItemCatalogs.Count">
                        <span id="@itemCatalogGroup.UploaderIconPicClientId" class="unstyled"></span>
                        <img src="@itemCatalogGroup.DefaultIconPicUrl" id="@(itemCatalogGroup.UploaderIconPicClientId)_pictureurl" />
                        
                        <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].IconPictureId"
                               id="@(itemCatalogGroup.UploaderIconPicClientId)_pictureid" />
                        

                    </td>
                    
                    <td rowspan="@itemCatalogGroup.ItemCatalogs.Count">
                        @{
                            var ppIndex = 0;
                        }
                        @foreach (var pp in itemCatalogGroup.ProductPictures)
                        {
                            <span id="@pp.ClientUploaderId" class="unstyled"></span>
                            <img src="@pp.DefaultProductPicUrl" id="@(pp.ClientUploaderId)_pictureurl" />
                            <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].ProductPictures[@ppIndex].Id"
                                        id="@(pp.ClientUploaderId)_pictureid" />
                            ppIndex++;
                        }
                    </td>
                </tr>
                        var itemCatalogIndex =1;

                        foreach (var itemCatalog in itemCatalogGroup.ItemCatalogs.Skip(1))
                        {
                     <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[@itemCatalogIndex].ProductAttributeValueId" value="@itemCatalog.ProductAttributeValueId" />
                            <input type="hidden" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[@itemCatalogIndex].ItemAttributeValueId" value="@itemCatalog.ItemAttributeValueId" />


                    <tr>
                        <td>
                            @itemCatalog.ItemAttributeValue.Value
                        </td>
                        <td>
                            <input type="text" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[@itemCatalogIndex].Stock" />
                          
                        </td>
                        <td>
                            <input type="text" name="ItemCatalogGroups[@itemCatalogGroupIndex].ItemCatalogs[@itemCatalogIndex].Price" />
                        </td>
                    </tr>
                    itemCatalogIndex++;
                }

                itemCatalogGroupIndex++;

            }

            @*@foreach (var itemCatalog in Model.ItemCatalogs)
            {
                <tr>
                    @if (Model.UseProductAttribute)
                    {
                        <td>
                            <span>@itemCatalog.ProductAttributeValue.Value</span>
                            <input type="hidden" name="ItemCatalogs[@itemindex].ProductAttributeValue.Id" value="@itemCatalog.ProductAttributeValue.Id" />
                        </td>
                    }
                    @if (Model.UseItemAttribute)
                    {
                        <td>
                            <span>@itemCatalog.ItemAttributeValue.Value</span>
                            <input type="hidden" name="ItemCatalogs[@itemindex].ItemAttributeValue.Id" value="@itemCatalog.ItemAttributeValue.Id" />
                        </td>
                    }
                    <td>
                        <input type="text" name="ItemCatalogs[@itemindex].Stock" />
                    </td>
                    <td>
                        <input type="text" name="ItemCatalogs[@itemindex].Price" />
                    </td>
                    <td rowspan="">
                        <span id="@itemCatalog.UploaderClientId" class="unstyled"></span>
                        <img src="@itemCatalog.DefaultIconPicUrl" id="@(itemCatalog.UploaderClientId)_pictureurl" />
                        <input type="hidden" name="ItemCatalogs[@itemindex].PictureId" id="@(itemCatalog.UploaderClientId)_pictureid" />
                    </td>
                </tr>

                itemindex++;
            }*@
        </table>


    </div>

</form>
<input type="button" value="@T(MessageName.InsertItem)" onclick="DoSave()" class="btn btn-primary" />
@section scripts{
   
    
    
    

<script type="text/javascript">

    function DoSave() {
        var jsObj = form2js('submitform');

        $.ajax({
            //url: "/Product/BatchUpdateInitProduct2",
            url: "/api/ProductApi/InsertItem",
            type: 'post',

            data: ko.toJSON(jsObj),
            contentType: 'application/json',
            success: function (result) {
                if (result.Status == "0") {


                    //$('#productcount').html(result.Message);
                    //$('#message').show();
                    alert("保存成功");

                } else {
                    alert(result.Message);
                }
            }
        });
    }
    $(document).ready(function () {

        var errorHandler = function (event, id, fileName, reason) {
            qq.log("id: " + id + ", fileName: " + fileName + ", reason: " + reason);
        };

        @foreach (var itemCatalog in Model.ItemCatalogGroups)
        {
            <text>
        var uploader_@(itemCatalog.UploaderIconPicClientId) = new qq.FineUploader({
            element: $('#@(itemCatalog.UploaderIconPicClientId)')[0],
            autoUpload: true,
            uploadButtonText: "Select Files",
            request: {
                endpoint: "/FineUploader/UploadFile",
                params: { "extraParam1": "true", "picSize": "100" }
            },
            callbacks: {
                onError: errorHandler,
                onComplete: function (id, fileName, responseJSON) {
                    if (responseJSON.success) {
                        var clientPictureId = "#@(itemCatalog.UploaderIconPicClientId)_pictureid";
                        var clientPictureUrl = "#@(itemCatalog.UploaderIconPicClientId)_pictureurl";
                        $(clientPictureId).val(responseJSON.OriginalPictureId);
                        $(clientPictureUrl).attr("src", responseJSON.NewPictureUrl);
                    }
                }
            }
        });
        </text>
        }


        @foreach (var itemCatalog in Model.ItemCatalogGroups)
        {
            foreach (var pp in itemCatalog.ProductPictures)
            {
                     <text>
         var uploader_@(pp.ClientUploaderId) = new qq.FineUploader({
             element: $('#@(pp.ClientUploaderId)')[0],
             autoUpload: true,
             uploadButtonText: "Select Files",
             request: {
                 endpoint: "/FineUploader/UploadFile",
                 params: { "extraParam1": "true", "picSize": "100" }
             },
             callbacks: {
                 onError: errorHandler,
                 onComplete: function (id, fileName, responseJSON) {
                     if (responseJSON.success) {
                         var clientPictureId = "#@(pp.ClientUploaderId)_pictureid";
                         var clientPictureUrl = "#@(pp.ClientUploaderId)_pictureurl";
                         $(clientPictureId).val(responseJSON.OriginalPictureId);
                         $(clientPictureUrl).attr("src",responseJSON.NewPictureUrl);
                     }
                 }
             }
         });
            </text>
            }

        }
    });
</script>

}

